package com.managermype.business;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.managermype.domain.Message;
import com.managermype.domain.MessageAccess;
import com.managermype.util.Error;

@Service
public class MessageBusiness {
	@Autowired
	private XmlReaderBusiness xmlReaderBusiness;

	@Autowired
	private MessageAccess messageAccess;

	private Map<String, Message> mMessages = new HashMap<String, Message>();

	public MessageBusiness() {
		xmlReaderBusiness = new XmlReaderBusiness();
		InputStream xmlResource = this.getClass().getResourceAsStream("/message.xml");
		messageAccess = new MessageAccess();
		messageAccess = (MessageAccess) xmlReaderBusiness.objectReaderXml(messageAccess,xmlResource);
		for (Message message : messageAccess.getMyMessages()) {
			mMessages.put(message.getId(), message);
		}
	}

	public String getInternalDescriptionMessage(String code) {
		return mMessages.get(code) != null ? mMessages.get(code).getInternalDescription()
				: mMessages.get(Error.GENERIC_ERROR.getErrorCode()).getInternalDescription();
	}

	public String getExternalDescriptionMessage(String code) {
		return mMessages.get(code) != null ? mMessages.get(code).getExternalDescription()
				: mMessages.get(Error.GENERIC_ERROR.getErrorCode()).getExternalDescription();
	}
}
